{{- $_ := set . "Version" ( splitList "/" .Values.global.doc_version | last ) }}
{{- $_ := set . "VersionURLNormalized" ( printf "%s" .Version | lower | replace "+" "-plus-" | replace "_" "-u-" ) }}
{{- $_ := set . "VersionDNSNormalized" ( printf "%s" .Version | lower | replace "+" "-plus-" | replace "_" "-u-" | replace "." "-dot-" ) }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
  annotations:
    "documentation/version": "{{ .Version }}"
    "werf.io/replicas-on-creation": "1"
  labels:
    service: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
spec:
  minReadySeconds: 5
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      service: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
  template:
    metadata:
      labels:
        service: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
    spec:
      imagePullSecrets:
        - name: deckhouse-registry-site
      priorityClassName: {{ pluck .Values.werf.env .Values.priorityClassName | first | default .Values.priorityClassName._default }}
      containers:
        - name: web
          command: ["nginx", "-g", "daemon off;"]
          image: {{ index .Values.werf.image "web" }}
          {{- include "resources" . | nindent 10 }}
          ports:
          - containerPort: 80
            name: http
            protocol: TCP
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx", "-s", "quit"]
          livenessProbe:
            httpGet:
              path: /healthz
              port: 80
              scheme: HTTP
            initialDelaySeconds: 5
            periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
spec:
  clusterIP: None
  selector:
    service: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
  ports:
  - name: http
    port: 80
    protocol: TCP
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
  updatePolicy:
    updateMode: "Auto"
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      service: {{ .Chart.Name }}-{{ .VersionDNSNormalized }}
